/* ----------------------------------------------------------------------------
* File: user_profile.cpp
*
* Desc: Definition of CUserProfile class
*
* Created: 19/02/2011, by Alexey Korshunov
* ---------------------------------------------------------------------------- */
#include "user_profile.h"
#include "file_logger.h"


/************************************************************************/
/*-----------------			CUserProfile section		----------------*/
/************************************************************************/

CUserProfile::CUserProfile()
{
	memset(&m_info, 0, sizeof(m_info));
}

CUserProfile::~CUserProfile()
{

}

// Read profile
int CUserProfile::ReadProfile()
{
	// TODO: read profile from database
	return BOX_SERVER_ERROR;
}

//CUserProfile::CUserProfile(CBoxServer *parent, unsigned int id):
//m_parent(parent)
//{
//	memset(&m_info, 0, sizeof(m_info));
//
//	char buf[32];
//	sprintf(buf, "./tcp_conn%d.log", id);
//	m_logger = (ILogger*)new CFileLogger(buf);
//}
//
//
//CUserProfile::~CUserProfile()
//{
//	// destroy all user box objects
//	VectorUserBoxesIterator it = m_list_boxes.begin();
//	for (; it != m_list_boxes.end(); ++it)
//		delete *it;
//	m_list_boxes.clear();
//
//	// Destroy file logger
//	delete m_logger;
//}
//
///************************************************************************/
///*	Desc:	Read profile from database                                  */
///************************************************************************/
//int CUserProfile::ReadProfile()
//{
//	// TODO:
//	// 1) connect to database
//	// 2) read profile for this user
//	// 3) build info about all boxes
//	return BOX_SERVER_OK;
//}
//
//
///************************************************************************/
///*	Desc:	Read property of files in all boxes                         */
///************************************************************************/
//int CUserProfile::ReadBoxesProperty()
//{
//	return BOX_SERVER_OK;
//}
//
///************************************************************************/
///*	Desc:	Build boxes property										*/
///************************************************************************/
//int CUserProfile::BuildUserInfo(void** data, size_t& size)
//{
//	int ret = BOX_SERVER_OK;
//
//	// Detect common size
//	size_t num_files = 0; 
//	VectorUserBoxesIterator it = m_list_boxes.begin();
//	for (; it != m_list_boxes.end(); ++it)
//		num_files += (*it)->GetCountFiles();
//	
//	user_info* uinfo = (user_info*)malloc(sizeof(user_info));
//	if (uinfo == NULL)
//		return BOX_SERVER_ERROR;
//
//	size_t num_boxes = m_list_boxes.size();
//	box_info* binfo = (box_info*)malloc(num_boxes * sizeof(box_info));
//	if (binfo == NULL)
//		return BOX_SERVER_ERROR;
//
//	// Build file info for each box
//	int i = 0;
//	for (it = m_list_boxes.begin(); it != m_list_boxes.end(); ++it)
//	{
//		file_info* finfo = NULL;
//		size_t num_files = 0;
//		ret = (*it)->BuildFileInfo((void**)&finfo, num_files);
//		if (ret != BOX_SERVER_OK)
//			return ret;
//
//		binfo[i].data = finfo;
//		binfo[i].num_files = num_files;
//		++i;
//	}
//	uinfo->data = binfo;
//	uinfo->num_boxes = num_boxes; 
//	*data = uinfo;
//
//	return BOX_SERVER_OK;
//}
//
//
///************************************************************************/
///*	Desc:	Get box info												*/
///************************************************************************/
//CUserBox* CUserProfile::GetBoxById(size_t id) const
//{
//	if (!m_list_boxes.empty())
//	{
//		if (id < m_list_boxes.size())
//			return (m_list_boxes[id]);
//		else
//			return NULL;
//	}
//
//	return NULL;
//}





/************************************************************************/
/*-----------------			CTestUser section		--------------------*/
/************************************************************************/

CTestUserProfile::CTestUserProfile(const char *username, CFileLogger *logger):
m_logger(logger),
m_fp(NULL)
{
	m_profile.username = (char*)malloc(strlen(username) + 1);
	if (m_profile.username != NULL)
		strncpy(m_profile.username, username, strlen(username) + 1);
}

CTestUserProfile::~CTestUserProfile()
{

}

int CTestUserProfile::ReadProfile()
{
	char file_name[512];
	char *variable = NULL, *value = NULL;
	unsigned int cur_box_id;
	unsigned int cur_file_id;

	sprintf(file_name, "%s%s.profile", CONFIG_DTRECTORY, m_profile.username);
	FILE *fp = fopen(file_name, "r");
	if (fp == NULL)
	{
		m_logger->Print("user box: ERROR - unable to open profile file %s\n", file_name);
		return BOX_SERVER_ERROR;
	}

	m_logger->Print("read profile from config file\n");
	char buf[CONFIG_STRING_LEN];
	while(fgets(buf, CONFIG_STRING_LEN, fp) != NULL)
	{
		if (buf[strlen(buf) - 1] == '\n')
			buf[strlen(buf) - 1] = '\0';

		if (buf[0] == '#')
			continue;

		if ((strlen(buf) > 2) && (buf[strlen(buf) - 1] != '='))
		{
			if (strchr(buf, '=') != NULL)
			{
				variable = strtok(buf, "=");
				value = strtok(NULL, "=");

				if (!strncmp("box_id", variable, 6))
				{
					cur_box_id = atoi(value);
					m_logger->Print("	box_id = [%d]\n", cur_box_id);

					user_box_info *binfo = new user_box_info;
					binfo->id = cur_box_id;
					m_profile.boxes[cur_box_id] = binfo;
				}
				else if (!strncmp("file_id", variable, 7))
				{
					cur_file_id = atoi(value);
					m_logger->Print("	file_id = [%d]\n", cur_file_id);

					file_info *finfo = new file_info;
					memset(finfo, 0, sizeof(file_info));
					finfo->id = cur_file_id;
					m_profile.boxes[cur_box_id]->files[cur_file_id] = finfo;
				}
				else if (!strncmp("file_name", variable, 9))
				{
					file_info *finfo = m_profile.boxes[cur_box_id]->files[cur_file_id];
					strncpy(finfo->name, value, strlen(value));
					finfo->name[strlen(value)] = '\0';
					m_logger->Print("	file_name = [%s]\n", value);
				}
			}
		}
	}
	fclose(fp);

	return BOX_SERVER_OK;
}

// Get profile info
int CTestUserProfile::GetUserInfo(user_info *uinfo)
{
	return BOX_SERVER_OK;
}

//CTestUserProfile::CTestUserProfile(CBoxServer *parent, unsigned int id):
//CUserProfile(parent, id)
//{
//}
//
//
//CTestUserProfile::~CTestUserProfile()
//{
//
//}
//
//
///************************************************************************/
///*	Desc:	Read profile from file                                      */
///************************************************************************/
//int CTestUserProfile::ReadProfile()
//{
//	//if (m_cfg_file == NULL)
//	//	return BOX_SERVER_ERROR;
//
//	//FILE *fp = fopen(m_cfg_file, "r");
//	//if (fp == NULL)
//	//	return BOX_SERVER_ERROR;
//
//	//// Read data
//	//while(!feof(fp))
//	//{
//
//	//}
//
//	//fclose(fp);
//	return BOX_SERVER_OK;
//}